/*
 * Copyright 2008 Federal Emergency Management Agency
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *    
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 *  
 */
package gov.fema.hazus.bdc.client.hazards.earthquake.hazard;

import gov.fema.hazus.bdc.client.common.ui.AbstractForm;
import gov.fema.hazus.bdc.client.common.ui.BuildingUpdate;
import gov.fema.hazus.bdc.client.common.ui.TextBoxIntegerValueBlurHandler;
import gov.fema.hazus.bdc.client.hazards.earthquake.EarthquakeConstants;
import gov.fema.hazus.bdc.shared.entity.Building;
import gov.fema.hazus.bdc.shared.entity.EarthquakeHazard;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

public class EarthquakeHazardForm extends AbstractForm implements BuildingUpdate {
	
	private Label soilType;
	private Label landslideSusceptibility;
	private Label liquefactionSusceptibility;
	private Label groundwaterDepth;
	
	// form fields
	private SoilTypeListBox formSoilType = new SoilTypeListBox();
	private LandslideSusceptibilityListBox formLandslideSusceptibility = new LandslideSusceptibilityListBox();
	private LiquefactionSusceptibilityListBox formLiquefactionSusceptibility = new LiquefactionSusceptibilityListBox();
	private TextBox formGroundwaterDepth = new TextBox();
	
	// ui components
	private DockPanel docker;
	private Grid table = new Grid(4,2);	
	
	// entities
	private EarthquakeHazard earthquakeHazard;
	
	// listeners
	private EarthquakeHazardListBoxChangeListener earthquakeHazardListBoxChangeListener;
	private EarthquakeHazardTextBoxKeyListener earthquakeHazardTextBoxKeyListener;
	private TextBoxIntegerValueBlurHandler textBoxIntegerValueBlurHandler;

	private EarthquakeConstants constants;
	
	public EarthquakeHazardForm(Building building)
	{
		// do parent
		super(building);
		
		// set up widgets
		docker = new DockPanel();
		
		// set up tab title
		constants = (EarthquakeConstants)GWT.create(EarthquakeConstants.class);
		
		tabTitle = constants.earthquakeHazardTabTitle();
		
		soilType = new Label(constants.earthquakeHazardSoilType());
		landslideSusceptibility = new Label(constants.earthquakeHazardLandslideSusceptibility());
		liquefactionSusceptibility = new Label(constants.earthquakeHazardLiquefactionSusceptibility());
		groundwaterDepth = new Label(constants.earthquakeHazardGroundwaterDepth());
		
		// add labels to form
		table.setWidget(0, 0, soilType);
		table.setWidget(1, 0, landslideSusceptibility);
		table.setWidget(2, 0, liquefactionSusceptibility);
		table.setWidget(3, 0, groundwaterDepth);
		
		// set up EarthquakeHazard
		earthquakeHazard = building.getEarthquakeHazard();
		earthquakeHazardListBoxChangeListener = new EarthquakeHazardListBoxChangeListener(earthquakeHazard);
		earthquakeHazardTextBoxKeyListener = new EarthquakeHazardTextBoxKeyListener(earthquakeHazard);
		textBoxIntegerValueBlurHandler = new TextBoxIntegerValueBlurHandler();
		
		// listeners
		loadListeners();
		
		// forms
		loadForms();
		
		// add fields to table
		table.setWidget(0, 1, formSoilType);
		table.setWidget(1, 1, formLandslideSusceptibility);
		table.setWidget(2, 1, formLiquefactionSusceptibility);
		table.setWidget(3, 1, formGroundwaterDepth);
		
		// add table to docker
		docker.add(table, DockPanel.CENTER);
		
		initWidget(docker);			
		
	}

	public void updateBuilding(Building building) {
		
		// entitites
		setBuilding(building);
		this.earthquakeHazard = building.getEarthquakeHazard();
		
		// listeners
		earthquakeHazardListBoxChangeListener.updateEarthquakeHazard(earthquakeHazard);
		earthquakeHazardTextBoxKeyListener.updateEarthquakeHazard(earthquakeHazard);
		
		// reload forms
		loadForms();
	}
	
	private void loadForms()
	{
		
		if( earthquakeHazard != null)
		{
			formSoilType.setListItem( earthquakeHazard.getSoilType() );
			formSoilType.setName(EarthquakeHazard.SOILTYPE);
			
			formLandslideSusceptibility.setListItem( earthquakeHazard.getLandslideSusceptibility() );
			formLandslideSusceptibility.setName(EarthquakeHazard.LANDSLIDE);
			
			formLiquefactionSusceptibility.setListItem( earthquakeHazard.getLiquefactionSusceptibility() );
			formLiquefactionSusceptibility.setName(EarthquakeHazard.LIQUEFACTION);
			
			String groundwaterDepth = null;
			if (earthquakeHazard.getGroundwaterDepth() != null) {
				groundwaterDepth = earthquakeHazard.getGroundwaterDepth().toString();
			}
			formGroundwaterDepth.setText( groundwaterDepth );
			formGroundwaterDepth.setName(EarthquakeHazard.GROUNDWATER);
		}
		
		if( building != null)
		{
	        if( building.getMode().equalsIgnoreCase(Building.READ_ONLY))
	        {
	            formSoilType.setEnabled(false);
	            formLandslideSusceptibility.setEnabled(false);
	            formLiquefactionSusceptibility.setEnabled(false);
	            formGroundwaterDepth.setEnabled(false);
	        }
	        else
	        {
	            formSoilType.setEnabled(true);
	            formLandslideSusceptibility.setEnabled(true);
	            formLiquefactionSusceptibility.setEnabled(true);
	            formGroundwaterDepth.setEnabled(true);
	        }
		}
	}
	
	private void loadListeners()
	{
		formSoilType.addChangeListener(earthquakeHazardListBoxChangeListener);
		formLandslideSusceptibility.addChangeListener(earthquakeHazardListBoxChangeListener);
		formLiquefactionSusceptibility.addChangeListener(earthquakeHazardListBoxChangeListener);
		formGroundwaterDepth.addFocusListener(earthquakeHazardTextBoxKeyListener);
		formGroundwaterDepth.addBlurHandler(textBoxIntegerValueBlurHandler);
	}

}
